package com.teradata.dashboard.adapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.teradata.dashboard.R;

public class KpiCategoryGroupAdapter extends BaseAdapter {

	//自定义图片Adapter以内部类形式存在于MainActivity中，方便访问MainActivity中的各个变量，特别是imgs数组
    private Context context;//用于接收传递过来的Context对象
    private JSONArray kpiGroups;
    public KpiCategoryGroupAdapter(Context context) {
        super();
        this.context = context;
    }
    public KpiCategoryGroupAdapter(Context context,JSONArray kpiGroups) {
        super();
        this.context = context;
        this.kpiGroups=kpiGroups;
    }


    /* (non-Javadoc)
     * @see android.widget.Adapter#getCount()
     */
    @Override
    public int getCount() {
        return Math.min(kpiGroups.length(),20);
    }

    /* (non-Javadoc)
     * @see android.widget.Adapter#getItem(int)
     */
    @Override
    public JSONObject getItem(int position) {
     
    	try {
			return kpiGroups.getJSONObject(position);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }

    /* (non-Javadoc)
     * @see android.widget.Adapter#getItemId(int)
     */
    @Override
    public long getItemId(int position) {
        return position;//categorys.getJSONObject(position).getString("kpiCategoryId");
    }

    /* (non-Javadoc)
     * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
 	   
 	   LayoutInflater inflater =LayoutInflater.from(context);
       convertView =inflater.inflate(R.layout.kpigroup,null);
        if(convertView!=null){
        	try {
				((TextView)convertView.findViewById(R.id.text_kpi_name)).setText(getItem(position).getString("KPI_GROUP_NAME"));
				((TextView)convertView.findViewById(R.id.text_bbs_cnt)).setText("100");
	        	((TextView)convertView.findViewById(R.id.text_KPI_DATAITEM_VALUE)).setText(getItem(position).getString("KPI_VALUE_DISP"));
	        	((TextView)convertView.findViewById(R.id.text_KPI_DATAITEM_LAST_FREQ_VALUE)).setText(getItem(position).getString("LAST_FREQ_VALUE"));
	        	((TextView)convertView.findViewById(R.id.text_KPI_DATAITEM_LAST_YEAR_VALUE)).setText(getItem(position).getString("LAST_YEAR_VALUE"));
	        	((TextView)convertView.findViewById(R.id.text_KPI_DATAITEM_LAST_YEAR_END_VALUE)).setText(getItem(position).getString("LAST_YEAR_END_VALUE"));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	
        	
        }
      
        return convertView;
 	   
      
    }

}


